package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class _46_Permutations {
    public static void main(String[] args) {
        Solution solution = new _46_Permutations().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        HashSet<Integer> set = new HashSet<>();

        private void dfs(int[] nums) {
            if (set.size() == nums.length) {
                res.add(new ArrayList<>(path));
                return;
            }
            for (int i = 0; i < nums.length; i++) {
                if (!set.contains(nums[i])) {
                    path.add(nums[i]);
                    set.add(nums[i]);
                    dfs(nums);
                    path.remove(path.size() - 1);
                    set.remove(nums[i]);
                }
            }
        }

        public List<List<Integer>> permute(int[] nums) {
            dfs(nums);
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}